﻿using Google.Protobuf.WellKnownTypes;
using Mobileterminal.Common;
using Mobileterminal.Dtos;
using Newtonsoft.Json;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Mobileterminal.Forms.Zyy
{
    public partial class UpReaction : UIEditForm
    {
        private ReacDTO app; // 使用你的DTO类
        public UpReaction(string ApplicationId)
        {
            InitializeComponent( );
            this.Load += async (sender, e) => await LoadDataAsync(ApplicationId);
        }
       
        private async Task LoadDataAsync(string ApplicationId)
        {
            var url = $"https://localhost:7224/api/ReceivingApplication/FanReceivingApplication?ApplicationId={ApplicationId}";
            var json = HttpClientHelper.ClientAsync("GET", url);
            var result = Newtonsoft.Json.JsonConvert.DeserializeObject<Result<List<ReacDTO>>>(await json);
            if (result != null && result.Data != null && result.Data.Count > 0)
            {
                app = result.Data[0];
                uiTextBox1.Text= app.Applicationnumber ;
                uiTextBox2.Text= app.Applicationunit ;
                uiTextBox3.Text=app.distributionunit;

                uiTextBox5.Text=app.Applicant ;
                uiTextBox6.Text=app.Urgency  ;
            }
        }
        public static List<ReacDTO> appsTypes = new List<ReacDTO>();

        private async void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                // 更新数据模型
                app.Applicationnumber = uiTextBox1.Text;
                app.Applicationunit = uiTextBox2.Text;
                app.distributionunit = uiTextBox3.Text;
              
                app.Applicant = uiTextBox5.Text;
                app.Urgency = uiTextBox6.Text;

                // 序列化并创建请求内容
                var json = JsonConvert.SerializeObject(app);
                HttpContent httpContent = new StringContent(json, Encoding.UTF8, "application/json");

                // 发送PUT请求
                var responseJson = await HttpClientHelper.ClientAsync(
                    "PUT",
                    "https://localhost:7286/api/ReceivingApplication/UpdateReceivingApplicationc",  // 修正URL拼写
                    true,
                    httpContent
                );

                if (string.IsNullOrEmpty(responseJson))
                {
                    MessageBox.Show("服务器返回空响应，请检查服务端状态或请求数据。");
                    return;
                }

                // 反序列化响应
                var result = JsonConvert.DeserializeObject<ReacDTO>(responseJson);
                if (result != null)
                {
                    MessageBox.Show("修改成功");
                    this.DialogResult = DialogResult.OK;  // 先设置结果
                    this.Close();

                }
                else
                {
                    MessageBox.Show("反序列化失败，响应内容格式不正确。");
                }
            }
            catch (HttpRequestException ex)
            {
                MessageBox.Show($"网络请求失败：{ex.Message}");
            }
            catch (JsonException ex)
            {
                MessageBox.Show($"解析响应失败：{ex.Message}");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"发生错误：{ex.Message}");
            }
        }

        private void UpReaction_Load(object sender, EventArgs e)
        {

        }

        private void uiLabel2_Click(object sender, EventArgs e)
        {

        }
    }
}
